package com.centriole.common.utils;

import java.util.Collection;
import java.util.HashMap;

public class PaasResult<T> {

    public static final String INNER_ERR_MSG = "内部异常";
    public static final String SUCCESS_MSG = "操作成功";
    public static final String FAIL_MSG = "操作失败";

    /**
     * code:
     * 0和200表示请求成功 ,
     * 500表示未意料到服务器内部错误异常，
     * 600表示 意料到的业务异常
     */
    private int code;
    private String msg;
    private T data;
    private int count;

    public PaasResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.count = 0;
    }

    public PaasResult(int code, String msg, T data, Integer count) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.count = count;
    }

    public static HashMap map(Integer code, String msg, Object data, Integer count) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("msg", msg);
        map.put("data", data);
        map.put("count", count);
        return map;
    }

    public static PaasResult success() {
        return new PaasResult(200, "ok", null, 0);
    }

    public static PaasResult success(String msg) {
        return new PaasResult(200, msg, null, 0);
    }

    public static <T>PaasResult success(T data) {
        return new PaasResult(200, "success", data, ifDataIsCollectionAndGetSize(data));
    }

    public static PaasResult success(String msg, Object data) {
        return new PaasResult(200, msg, data, ifDataIsCollectionAndGetSize(data));
    }

    public static PaasResult success(String msg, Object data, Integer count) {
        return new PaasResult(200, msg, data, count);
    }

    public static PaasResult successWithCode0(String msg, Object data, Integer count) {
        return new PaasResult(0, msg, data, count);
    }

    public static PaasResult successWithCode0(String msg, Object data) {
        return new PaasResult(0, msg, data, ifDataIsCollectionAndGetSize(data));
    }

    public static PaasResult fail(String msg) {
        return new PaasResult(600, msg, null, 0);
    }

    public static PaasResult error(String msg) {
        return new PaasResult(500, msg, null, 0);
    }

    /**
     * 如果data是一个集合类型，那么会获取到该集合的size大小
     *
     * @param data
     * @return int 集合大小
     */
    private static int ifDataIsCollectionAndGetSize(Object data) {
        if (data instanceof Collection) {
            return ((Collection) data).size();
        }
        return 0;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

}
